<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@page import="java.util.*"%>
<%@page import="java.text.DecimalFormat"%>
<%@ page import="java.sql.*"%>
<jsp:useBean id="db" class="ctm.javacode.dbBean" />
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>MenuDemo</title>
<script language="Javascript" type="text/javascript">
	function set() {
		var wight = parseFloat(document.getElementById('wight').innerHTML);
		var g_kgBW = parseFloat(document.getElementById('g_kgBW').value);
		var proportion_fat = parseFloat(document
				.getElementById('proportion_fat').value) / 100;// 脂肪百分比
		var calofday = parseFloat(document.getElementById('calofday').innerHTML);

		//三大營養素
		var prot = Math.round(wight * g_kgBW);
		var proportion_prot = (prot * 4) / calofday;
		var fat = Math.round(calofday * proportion_fat / 9);
		var proportion_cho = 1 - proportion_fat - proportion_prot;
		var cho = Math.round(calofday * proportion_cho / 4);
		var proportion_prot = Math.round((proportion_prot * 100) * 10) / 10// 蛋白質百分比
		proportion_cho = Math.round((proportion_cho * 100) * 10) / 10; // 醣類百分比

		document.getElementById('prot').innerHTML = prot.toString();
		document.getElementById('proportion_prot').innerHTML = proportion_prot
				.toString();
		document.getElementById('fat').innerHTML = fat.toString();
		document.getElementById('proportion_cho').innerHTML = proportion_cho
				.toString();
		document.getElementById('cho').innerHTML = cho.toString();
		document.getElementById('proportion_prot').innerHTML = proportion_prot
				.toString();
		document.getElementById('proportion_cho').innerHTML = proportion_cho
				.toString();

		//六大營養素
		var quantity_Milk = parseFloat(document.getElementById('quantity_Milk').value);
		var quantity_Veg = parseFloat(document.getElementById('quantity_Veg').value);
		var quantity_Fruit = parseFloat(document
				.getElementById('quantity_Fruit').value);
		// 低脂奶類: prot 每份8g、fat每份4g、cho每份12g
		// 蔬菜:    prot 每份1g、fat每份0g、cho每份5g 
		// 水果:    prot 每份0g、fat每份0g、cho每份15g
		// 穀類:    prot 每份2g、fat每份0g、cho每份15g
		// 豆魚肉蛋: prot 每份7g、fat每份5g、cho每份0g
		// 油脂:    prot 每份0g、fat每份5g、cho每份0g
		var quantity_Grain = (cho - quantity_Milk * 12 - quantity_Veg * 5 - quantity_Fruit * 15) / 15;
		var decimal = quantity_Grain - parseInt(quantity_Grain);
		if (decimal <= 0.2)
			quantity_Grain = parseInt(quantity_Grain);
		else if (decimal >= 0.8)
			quantity_Grain = parseInt(quantity_Grain) + 1;
		else
			quantity_Grain = parseInt(quantity_Grain) + 0.5;

		var quantity_Meat = (prot - quantity_Milk * 8 - quantity_Veg - quantity_Grain * 2) / 7;
		decimal = quantity_Meat - parseInt(quantity_Meat);
		if (decimal <= 0.2)
			quantity_Meat = parseInt(quantity_Meat);
		else if (decimal >= 0.8)
			quantity_Meat = parseInt(quantity_Meat) + 1;
		else
			quantity_Meat = parseInt(quantity_Meat) + 0.5;

		var quantity_Fat = (fat - quantity_Milk * 4 - quantity_Meat * 5) / 5;
		decimal = quantity_Fat - parseInt(quantity_Fat);
		if (decimal <= 0.2)
			quantity_Fat = parseInt(quantity_Fat);
		else if (decimal >= 0.8)
			quantity_Fat = parseInt(quantity_Fat) + 1;
		else
			quantity_Fat = parseInt(quantity_Fat) + 0.5;

		document.getElementById('quantity_Grain').innerHTML = quantity_Grain
				.toString();
		document.getElementById('quantity_Meat').innerHTML = quantity_Meat
				.toString();
		document.getElementById('quantity_Fat').innerHTML = quantity_Fat
				.toString();
	}
</script>

<style type="text/css">
body,td,th {
	color: #000000;
	font-size: 20px;
	font-family: "標楷體";
}

.HEAD_STYLE {
	font-weight: bold;
	color: #437A27;
	font-family: "標楷體";
	font-size: 30px;
}

span {
	color: #000000;
	font-size: 24px;
	font-family: "標楷體";
	font-weight: bold;
}

.button {
	border: 1px;
	padding-top: 4px;
	padding-right: 7px;
	padding-bottom: 4px;
	padding-left: 7px;
	background-color: #468129;
	color: #FFFFFF;
	font-size: 16px;
	border-color: #3C7023;
}

body {
	background-image: url(img/grass.jpg);
	background-position: center top;
	background-color: #FFFFFF;
	background-repeat: repeat;
}

a:link {
	text-decoration: none;
	font-family: Arial;
	font-size: 20px;
	color: #FFFFFF;
}

a:visited {
	text-decoration: none;
	font-family: Arial;
	font-size: 20px;
	color: #FFFFFF;
}

a:hover {
	text-decoration: none;
	font-family: Arial;
	font-size: 20px;
	color: #000000;
}

.ind:link {
	color: #535353;
	text-decoration: none;
	font-family: "標楷體";
	font-size: 18px;
}

.ind:visited {
	color: #535353;
	text-decoration: none;
	font-family: "標楷體";
	font-size: 18px;
}

.ind:hover {
	color: #BB280F;
	text-decoration: none;
	font-family: "標楷體";
	font-size: 18px;
}

.TEXT_STYLE {
	font-family: "標楷體";
	color: #437A27;
	font-size: 24px;
}

.TEXT_STYLE_2 {
	font-family: "標楷體";
	color: #000000;
	font-size: 24px;
}

.BOTTOM_STYLE {
	color: #FFFFFF;
	font-family: Arial;
	font-size: 20px;
}

.LEFT_BUTTON {
	font-family: "標楷體";
	font-size: 30px;
}
</style>

</head>
<body alink="#FF0000" vlink="#FF0000">

	<%
		double calofday = Double.parseDouble(request.getSession()
				.getAttribute("calofday").toString());

		double cho = Double.parseDouble(request.getSession()
				.getAttribute("cho").toString());
		double fat = Double.parseDouble(request.getSession()
				.getAttribute("fat").toString());
		double prot = Double.parseDouble(request.getSession()
				.getAttribute("prot").toString());

		double proportion_prot = Double.parseDouble(request.getSession()
				.getAttribute("proportion_prot").toString());
		double proportion_fat = Double.parseDouble(request.getSession()
				.getAttribute("proportion_fat").toString());
		double proportion_cho = Double.parseDouble(request.getSession()
				.getAttribute("proportion_cho").toString());

		double quantity_Milk = Double.parseDouble(request.getSession()
				.getAttribute("quantity_Milk").toString());
		double quantity_Veg = Double.parseDouble(request.getSession()
				.getAttribute("quantity_Veg").toString());
		double quantity_Fruit = Double.parseDouble(request.getSession()
				.getAttribute("quantity_Fruit").toString());
		double quantity_Grain = Double.parseDouble(request.getSession()
				.getAttribute("quantity_Grain").toString());
		double quantity_Meat = Double.parseDouble(request.getSession()
				.getAttribute("quantity_Meat").toString());
		double quantity_Fat = Double.parseDouble(request.getSession()
				.getAttribute("quantity_Fat").toString());

		DecimalFormat df = new DecimalFormat("0");
		DecimalFormat df2 = new DecimalFormat("#.#");

		ArrayList<Boolean> foodlist = (ArrayList<Boolean>) request
				.getSession().getAttribute("foodlist");
		ArrayList<Boolean> foodweightlist = (ArrayList<Boolean>) request
				.getSession().getAttribute("foodweightlist");
		//double calofday = Double.parseDouble(request.getSession().getAttribute("calofday").toString()) ;
		double calofsugar = Double.parseDouble(request.getSession()
				.getAttribute("calofsugar").toString());
		double caloffat = Double.parseDouble(request.getSession()
				.getAttribute("caloffat").toString());
		double calofprotein = Double.parseDouble(request.getSession()
				.getAttribute("calofprotein").toString());
		double calofdish_userChoose = Double.parseDouble(request
				.getSession().getAttribute("calofdish_userChoose")
				.toString());
		double calofdishmax = calofday / 3 * 1.1;
		double calofdishmin = calofday / 3 * 0.9;

		double gofMilk = Double.parseDouble(request.getSession()
				.getAttribute("gofMilk").toString());
		double gofEggBeanFish = Double.parseDouble(request.getSession()
				.getAttribute("gofEggBeanFish").toString());
		double gofGrain = Double.parseDouble(request.getSession()
				.getAttribute("gofGrain").toString());
		double gofFruit = Double.parseDouble(request.getSession()
				.getAttribute("gofFruit").toString());
		double gofVeg = Double.parseDouble(request.getSession()
				.getAttribute("gofVeg").toString());
		double gofGrease = Double.parseDouble(request.getSession()
				.getAttribute("gofGrease").toString());
		double dishWeight = gofMilk + gofEggBeanFish + gofGrain + gofFruit
				+ gofVeg + gofGrease;
		double gofGrainCho = Double.parseDouble(request.getSession()
				.getAttribute("gofGrainCho").toString());
		double gofGrainFat = Double.parseDouble(request.getSession()
				.getAttribute("gofGrainFat").toString());
		double gofGrainPro = Double.parseDouble(request.getSession()
				.getAttribute("gofGrainPro").toString());
		double gofMilkCho = Double.parseDouble(request.getSession()
				.getAttribute("gofMilkCho").toString());
		double gofMilkFat = Double.parseDouble(request.getSession()
				.getAttribute("gofMilkFat").toString());
		double gofMilkPro = Double.parseDouble(request.getSession()
				.getAttribute("gofMilkPro").toString());
		double gofEggBeanFishCho = Double.parseDouble(request.getSession()
				.getAttribute("gofEggBeanFishCho").toString());
		double gofEggBeanFishFat = Double.parseDouble(request.getSession()
				.getAttribute("gofEggBeanFishFat").toString());
		double gofEggBeanFishPro = Double.parseDouble(request.getSession()
				.getAttribute("gofEggBeanFishPro").toString());
		double gofFruitCho = Double.parseDouble(request.getSession()
				.getAttribute("gofFruitCho").toString());
		double gofVegCho = Double.parseDouble(request.getSession()
				.getAttribute("gofVegCho").toString());
		double gofGreaseCho = Double.parseDouble(request.getSession()
				.getAttribute("gofGreaseCho").toString());
		double gofFruitFat = Double.parseDouble(request.getSession()
				.getAttribute("gofFruitFat").toString());
		double gofVegFat = Double.parseDouble(request.getSession()
				.getAttribute("gofVegFat").toString());
		double gofGreaseFat = Double.parseDouble(request.getSession()
				.getAttribute("gofGreaseFat").toString());
		double gofFruitPro = Double.parseDouble(request.getSession()
				.getAttribute("gofFruitPro").toString());
		double gofVegPro = Double.parseDouble(request.getSession()
				.getAttribute("gofVegPro").toString());
		double gofGreasePro = Double.parseDouble(request.getSession()
				.getAttribute("gofGreasePro").toString());
		// calofday再比出六大類適合份數
		// =============
		//
		// 穀類: 提供Cho 每份15g
		// 豆魚肉蛋: 提供Pro 每份7g
		// 低脂乳品: 提供Pro 每份8g
		// 蔬菜類: 提供Cho 每份5g
		// 水果類: 提供每份Cho 每份15g
		// 油脂與堅果種子類: 提供Fat 每份5g
		// 
		db.setDataSource("jdbc/ctmpllab");
		db.Connected();
		int calofday_standard = (int) (calofday / 100) * 100;

		if (calofday_standard > 2700)
			calofday_standard = 2700;

		String sqlQuery = "select * from type6foodtable where kcal='"
				+ Integer.toString(calofday_standard) + "'";
		ResultSet rset = db.ExcuteQuery(sqlQuery);
		rset.next();
	%>

	<table width="100%" border="0">

		<tr>
			<td><p>&nbsp;</p></td>
		</tr>
		<tr>
			<td width="5%">&nbsp;</td>
			<td bgcolor="#FFFFFF">

				<table width="100%" border="0">
					<tr>
						<td background="img/herbal.jpg">
							<p>&nbsp;</p>
							<p>&nbsp;</p>
							<p>&nbsp;</p>
							<p>&nbsp;</p>
							<p>&nbsp;</p>

						</td>
					</tr>
					<tr>
						<td>
							<table bgcolor="#437A27" width="100%" border="0">
								<tr>
									<td>&nbsp;</td>
									<td width="20%" align="center"><a href="main.jsp"
										target="_parent">回到主選單</a></td>
								</tr>
							</table>
						</td>
					</tr>
					<tr>
						<td>
							<table width="100%">
								<tr>
									<td valign="top">
										<table width="100%">
											<tr>
												<td class="HEAD_STYLE" align="center">菜單熱量評量</td>
											</tr>
											<tr>
												<td>

													<table width="90%" align="center" border="1">
														<tr>
															<td align="center"><span>三大營養素</span></td>
															<td align="center"><span>建議熱量</span></td>
															<td align="center"><span>實際熱量</span></td>

														</tr>
														<tr>
															<td align="center"><span>蛋白質</span></td>
															<td align="center"><span id="wight"
																style="display: none"><%=df.format(prot)%></span> <span
																id="prot"><%=df.format(prot)%></span><span>克(</span> <span
																id="proportion_prot"><%=df2.format(proportion_prot)%></span><span>%)(</span>
																<input type="text" id="g_kgBW" size="2"
																class="input_text" style="text-align: center"
																onchange="set()" value="1"> <span>g/kgBW)</span></td>
															<td align="center"><%=df.format(calofprotein)%>大卡</td>
														</tr>
														<tr>
															<td align="center"><span>脂肪</span></td>
															<td align="center"><span id="fat"><%=df.format(fat)%></span><span>克(</span>
																<input type="text" id="proportion_fat" size="2"
																class="input_text" onchange="set()"
																style="text-align: center"
																value=<%=df2.format(proportion_fat)%>> </span><span>%)</span></td>
															<td align="center"><%=df.format(caloffat)%>大卡</td>
														</tr>
														<tr>
															<td align="center"><span>醣類</span></td>
															<td align="center"><span id="cho"><%=df.format(cho)%></span><span>克(</span>
																<span id="proportion_cho"><%=df2.format(proportion_cho)%></span><span>%)</span></td>
															<td align="center"><%=df.format(calofsugar)%>大卡</td>
														</tr>
														<tr>
															<td align="center"><span>總共</span></td>
															<td align="center"><span id="calofday"><%=df.format(calofday)%></span><span>大卡</span></td>
															<td align="center"><%=df.format(calofdish_userChoose)%>大卡</td>
														</tr>
													</table> <br> <br> <br>
													<table width="90%" align="center" border="1">
														<tr>
															<td align="center"><span>六大類食物</span></td>
															<td align="center"><span>建議份量</span></td>
															<td align="center"><span>實際份量</span></td>

														</tr>

														<tr>
															<td align="center"><span>低脂奶類</span></td>
															<td align="center"><input type="text"
																id="quantity_Milk" size="2" class="input_text"
																onchange="set()" style="text-align: center"
																value=<%=df2.format(quantity_Milk)%>></td>
															<td align="center"><%=df2.format(gofMilkPro / 8)%></td>
														</tr>

														<tr>
															<td align="center"><span>蔬菜類</span></td>
															<td align="center"><input type="text"
																id="quantity_Veg" size="2" class="input_text"
																onchange="set()" style="text-align: center"
																value=<%=df2.format(quantity_Veg)%>></td>
															<td align="center"><%=df2.format(gofVegCho / 5)%></td>
														</tr>

														<tr>
															<td align="center"><span>水果類</span></td>
															<td align="center"><input type="text"
																id="quantity_Fruit" size="2" class="input_text"
																onchange="set()" style="text-align: center"
																value=<%=df2.format(quantity_Fruit)%>></td>
															<td align="center"><%=df2.format(gofFruitCho / 15)%></td>
														</tr>

														<tr>
															<td align="center"><span>全穀根莖類</span></td>
															<td align="center"><span id="quantity_Grain"><%=df.format(quantity_Grain)%></span>
															</td>
															<td align="center"><%=df2.format(gofGrainCho / 15)%></td>
														</tr>

														<tr>
															<td align="center"><span>豆魚肉蛋類</span></td>
															<td align="center"><span id="quantity_Meat"><%=df.format(quantity_Meat)%></span>
															</td>
															<td align="center"><%=df2.format(gofEggBeanFishPro / 7)%></td>
														</tr>

														<tr>
															<td align="center"><span>油脂及堅果種子類</span></td>
															<td align="center"><span id="quantity_Fat"><%=df.format(quantity_Fat)%></span>
															</td>
															<td align="center"><%=df2.format(gofGreaseFat / 5)%></td>
														</tr>
													</table>



												</td>
											</tr>
											<tr>

												<td align="center">
														<input class="button" type="button" value="回到主選單"
															onClick="location='main.jsp'" />
												</td>

											</tr>

										</table>
									</td>
								</tr>
							</table>


						</td>
					</tr>
					<tr>
						<td><p>&nbsp;</p></td>
					</tr>
					<tr>
						<td bgcolor="#437A27"><div align="center"
								class="BOTTOM_STYLE">Copyright@ITRI and CCU_PLlab</div></td>

					</tr>

				</table>
			</td>
			<td width="5%">&nbsp;</td>
		</tr>


	</table>
	<p>&nbsp;</p>





</body>
</html>






